/*-*-C-*-
 * Document data formats
 */

#ifndef __defined_document_h
#define __defined_document_h

/*
 * Each resource (object) is represented by one of these.  The
 * object's name & class is stored in the object header itself.  We
 * talk about "resources" as well as "objects" to make the distinction
 * between ResEd's private data and the object data itself
 *
 * The resource record is pinned down in memory, and won't move.  It's
 * address is used as the opaque "object ID" in transactions with the CSE.
 * The object header & data is in a separate malloc block which will
 * change address whenever the object is updated by a CSE.  In particular
 * the address of the name string (used by the shell to display the object
 * name) will change at this point.
 */

typedef struct _resourcerec
{
    RectRec spritebbox;                 /* icon's bounding box on document window work area */
    RectRec namebbox;                   /* name's bounding box on document window work area */
    Bool selected;                      /* set if this template is selected in doc viewer */
    struct _documentrec * owner;        /* document rec that owns me */

    ResourceFileObjectTemplateHeaderPtr object;
                                        /* object header fby data */
    ClassPtr class;                     /* Class relating to this object */
    Bool editing;                       /* TRUE if currently being edited by the CSE */
    Bool modified;                      /* TRUE if the CSE needs to return the data to us */
    Bool importing;                     /* TRUE while completing message import */
    ResourceFileObjectTemplateHeaderPtr original;
                                        /* object data before messages were imported */
    struct _resourcerec *next;          /* Link them together */
} ResourceRec, *ResourcePtr;

/*
 * Each document, and its associated viewer window, is represented
 * by one of these structures.
 *
 * The document record is pinned down in memory and won't move.  It's address
 * is used as the opaque "document ID" in transactions with CSEs.
 *
 * A list of malloced ResourceRecs is used to keep track of the objects in the
 * document.  It is not reordered (expect due to deletion) and new resources are
 * always added at the end.  Hence when we come to save the file we can
 * keep the objects in their original order - important for message
 * export/import.
 *
 * The array of ResourcePtrs holds pointers into the above list.  Its purpose
 * is to map from the "nth" object in the document display to the
 * correct malloced ResourceRec.  This array is coalesced when
 * resources are deleted, and it is kept sorted according to the
 * desired display ordering.
 */

typedef struct _documentrec
{
    char title[FILENAMELEN];            /* Also used for titlebar indir. text */
    int namelength;                     /* length of name part of the above */
    Bool fullpath;                      /* Filename is a full path */
    Bool modified;                      /* File needs saving */
    char timestamp[8];                  /* Last-known timestamp */

    WindowRec window;                   /* window desc for document's viewer */

    int versionnumber;                 /* Version number of this file */
    int objectoffset;                  /* So we can produce the same header when we save */
    int numresources;                   /* # of slots currently used in arrays below */
    ResourcePtr resources;              /* Pointer to first resource */
    int mappingmax;                     /* size allocated to array below */
    ResourcePtr *mapping;               /* for sorting the display */

    int numselected;                    /* number selected resources in the window */
    Bool weakselection;                 /* selection should be deselected when menu closes */
    int gridwidth, gridheight;          /* current rows/cols of icon layout */
    Bool internal;                      /* this is an internal document (palette) */
} DocumentRec, *DocumentPtr;


extern Bool document_disambiguate_name (DocumentPtr doc, char *name);
extern error * document_lose_selection (DocumentPtr doc);
extern int document_file_size (DocumentPtr doc, Bool selection);
extern error * document_load_prototypes (void);
extern error * document_modified (DocumentPtr doc, Bool modified);
extern error * document_change_filename (DocumentPtr doc, char *filename, Bool redraw);
extern error * document_create (DocumentPtr *ret, char *filename);
extern error * document_create_palette (void);
extern error * document_add_resource (DocumentPtr doc, ResourcePtr *ret, Bool isnew);
extern error * document_delete_selection (DocumentPtr doc);
extern error * document_delete_resource (ResourcePtr res);
extern error * document_open_window (WindowPtr win, DocumentPtr doc);
extern error * document_close_window (DocumentPtr doc);
extern error * document_claim_focus (DocumentPtr doc);
extern error * document_redraw_window (WindowRedrawPtr redraw, DocumentPtr doc);
extern int document_negotiate_filetype (int *mypref, int *yourpref);
extern error * document_rename_resource (DocumentPtr doc, ResourcePtr res, char *newname);
extern error * document_mouse_click (MouseClickPtr mouse, unsigned int modifiers, DocumentPtr doc);
extern error * document_claim_drag (DocumentPtr doc,             /* the document in this window */
                                     int windowhandle,           /* window handle of receiver/claimant */
                                     MessageDraggingPtr msg,     /* may contain different widow handle */
                                     int *claimref);             /* update with new myref, else 0 */
extern void document_free_all (void);
extern DocumentPtr document_lookup_by_filename (char *name);
extern error * document_raise_window (DocumentPtr doc);
extern error * document_key_pressed (DocumentPtr doc, KeyPressPtr key, Bool *consumed);
extern error * document_load (char *buf, int size, DocumentPtr doc, Bool silent, Bool select, Bool isnew);
extern error * document_load_file (char *filename, DocumentPtr doc, Bool silent, Bool select, Bool isnew);
extern error * document_close_palette (void);
extern error * document_merge_palette (char *filename);
extern Bool document_exists (DocumentPtr doc);
extern error * document_copy_resource (DocumentPtr doc, ResourcePtr res, char *newname);
extern error * document_recover_object_data (DocumentPtr doc, ResourcePtr res, int address, int size);
extern error * document_recover_resource (DocumentPtr doc, ResourcePtr res);
extern error * document_recover_document (DocumentPtr doc);
extern Bool document_help_text (
    DocumentPtr doc,
    PointPtr mouse,
    char *reply
);


#endif
